package br.com.sgpv2.hibernate.contrato;

import java.util.Collections;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import br.com.sgpv2.entity.pessoa.Contrato;
import br.com.sgpv2.entity.pessoa.Pessoa;
import br.com.sgpv2.hibernate.infra.SGPGenericDAO;

public class ContratoHibernateDAO extends SGPGenericDAO<Contrato> implements
		ContratoRepository {

	/** Construtor padrão. */
	@Autowired
	public ContratoHibernateDAO(
			@Qualifier("sessionFactory") SessionFactory factory) {
		super(Contrato.class);
		super.setSessionFactory(factory);
	}

	public List<Contrato> consultarPorTitular(Pessoa pessoa) {
		Criteria criteria = createCriteria();
		criteria.add(Restrictions.eq("pessoa.id", pessoa.getId()));
		return Collections.checkedList((List<Contrato>) criteria.list(),
				Contrato.class);
	}

	public Contrato buscarPorMatricula(String matriculaContrato) {
		Criteria criteria = createCriteria();
		criteria.add(Restrictions.eq("matricula", matriculaContrato));
		return (Contrato) criteria.uniqueResult();
	}
}
